import axios, { ResDataType } from './api'

export async function getQuestionService(id: string): Promise<ResDataType> {
  const url = `/api/question/${id}`
  const data = (await axios.get(url)) as ResDataType

  return data
}

export async function createQuestionService(): Promise<ResDataType> {
  const url = '/api/question/'
  const data = (await axios.post(url)) as ResDataType

  return data
}

type SearchOpt = {
  keyword: string
  isStar: boolean
  isDeleted: boolean
  page: number
  pageSize: number
}
export async function getQuestionListService(opt: Partial<SearchOpt> = {}): Promise<ResDataType> {
  const url = '/api/question'
  const data = (await axios.get(url, { params: opt })) as ResDataType
  // console.log(data)
  return data
}

export async function updateQuestionService(
  id: string,
  opt: { [key: string]: any }
): Promise<ResDataType> {
  const url = `/api/question/${id}`
  const data = (await axios.patch(url, opt)) as ResDataType
  // console.log(data)
  return data
}
export async function duplicateQuestionService(id: string): Promise<ResDataType> {
  const url = `/api/question/duplicate/${id}`
  const data = (await axios.post(url)) as ResDataType
  // console.log(data)
  return data
}
export async function deleteQuestionService(ids: string[]): Promise<ResDataType> {
  const url = '/api/question'
  const data = (await axios.delete(url, { data: { ids } })) as ResDataType
  // console.log(data)
  return data
}
